package com.youkeda.yikao.web.control;

import com.google.code.kaptcha.Constants;
import com.youkeda.yikao.web.dao.ZhangyankunSpringUserMapper;
import com.youkeda.yikao.web.dataobject.ZhangyankunSpringUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
public class LoginControl {
    @Autowired
    ZhangyankunSpringUserMapper zhangyankunSpringUserMapper;

    public static String SQL = "hkjkj";

    /**
     * 向数据库中添加数据*/
    @RequestMapping(path = "/add")
    @ResponseBody
    public String add() {
        ZhangyankunSpringUser user = new ZhangyankunSpringUser();

        user.setName("admin@admin.com");
        user.setPsd("Batch123");

        zhangyankunSpringUserMapper.insert(user);

        return "success";
    }
/**
 * 冲数据库中筛选出用户*/
    public ZhangyankunSpringUser select(String name) {

        ZhangyankunSpringUser user = zhangyankunSpringUserMapper.selectByPrimaryKey(name);
        System.out.println(user.getName());
        return user;
    }

/**
 * 登录验证*/
    @RequestMapping("login")
    public ModelAndView login(@RequestParam("captcha") String captcha, ZhangyankunSpringUser user, HttpSession session) {
        if (!captcha.equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))){
            return new ModelAndView("redirect:signup1.html");
        }
        //调用select方法，从数据库中去出用户
        ZhangyankunSpringUser user1 = select(user.getName());
        //判断输入的内容的样式是email、个性域名还是电话号码
        String email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        String telephone = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
        String modin = "[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?";

        //如果去除的用户名为空，返回错误页面
        if (user1 == null) {
            return new ModelAndView("redirect:/error.html");
        }

        //使用matches判断正则表达式是email还是电话还是个性域名
        if (user.getName().matches(email)) {
            if (user1.getPsd().equals(user.getPsd())) {
                session.setAttribute("user", user);
                //返回主页
                return new ModelAndView("redirect:/test1.html");
            } else {
                //返回登录页面
                return new ModelAndView("redirect:/login.html");
            }
        } else {
            //返回错误页面
            return new ModelAndView("redirect:/error.html");
        }

    }

    @RequestMapping(path = "/loginout")
    @ResponseBody
    public String loginOut(HttpSession session) {
        session.removeAttribute("user");
        return "注销登录";
    }

    @RequestMapping("/getuser")
    @ResponseBody
    public ZhangyankunSpringUser getUser(@SessionAttribute("user") ZhangyankunSpringUser user) {
        return user;
    }

    @RequestMapping("/test")
    public String test(@RequestParam("user") String user, @RequestParam("telephone") String telephone,
                             @RequestParam("email") String email, @RequestParam("introduce") String introduce){
        List<String> documents=new ArrayList<>();
        documents.add(user);
        documents.add(telephone);
        documents.add(email);
        documents.add(introduce);

        return "signup1.html";
    }
}
